"""Example web view for application factory."""


from flask import Blueprint, jsonify

from flask import request
from application_factory.service import *
from application_factory.vul_scan_service import vul_scan_entry_service, vul_scan_immediately, remove_safe_detect_service
from application_factory import init_sys_service, configure_service, nmap_discovery_call
import results
web_bp = Blueprint("web_bp", __name__)


@web_bp.route("/")
def index():
    """Say hi!.

    :url: /
    :returns: hi!
    """
    return "hi! 访问正确!"


@web_bp.route("/inventory_discovery")
def inventory_discovery():
    """
    立即进行资产扫描
    :return:
    """
    res = nmap_discovery_call.cal_service()
    return jsonify(res)


# 资产(inventory) 初始化配置
# 参数:
#   ip: 资产ip
#   username: 用户名
#   password: 密码
@web_bp.route("/init_configure")
def init_configure():
    """
    资产初始化设置
    需要传入参数inventory_ip, username, password
    :return:
    """
    inventory_ip = request.args.get('ip')
    username = request.args.get('username')
    password = request.args.get('password')
    port = request.args.get("port")
    result = linux_inventory_init_configure(inventory_ip, username, password)
    return jsonify(result)


# 安全检查配置, 配置什么时候进行漏扫
# 参数:
#   time: 设置的时间，e.g 22:30 晚上10点30分
#   id: 资产id
@web_bp.route("/safe_detect_configure")
def safe_detect_configure():
    """
    配置漏扫时间
    :return:
    """
    configure_time = request.args.get('time')
    id = request.args.get("id")
    result = vul_scan_entry_service(configure_time, id)
    return jsonify(result)


@web_bp.route("/remove_safe_detect")
def remove_safe_detect():
    """
    删除资产的漏扫规则
    :return:
    """
    id = request.args.get("id")
    res = remove_safe_detect_service(id)
    return jsonify(res)


# 立即进行漏扫
# 参数:
#   id: 资产编号
@web_bp.route("/safe_detect_immediately")
def safe_detect_immediately():
    """
    资产漏洞扫描，立即执行
    :return:
    """
    id = request.args.get("id")
    return jsonify(vul_scan_immediately(id))


@web_bp.route("/usability_detect")
def usability_detect():
    """
    资产可用性探测
    :return:
    """
    ip = request.args.get("ip")
    username = request.args.get("username")
    passwd = request.args.get("password")
    port = request.args.get("port")
    if ip is None or username is None or passwd is None or port is None:
        return jsonify(results.req_info_incomplete_result)
    # 先做Linux可用性探测
    res = init_sys_service.usability_detect(ip, username, passwd, port)
    if res['code'] != 200:
        res = init_sys_service.win_usability_detect(ip, username, passwd, port)
    return jsonify(res)


# 接口作用： 获取目标主机的主机号
# 参数内容： ip、 username、 passwd、 port
@web_bp.route("/hostname_detect")
def hostname_detect():
    ip = request.args.get("ip")
    username = request.args.get("username")
    passwd = request.args.get("password")
    port = request.args.get("port")
    if ip is None or username is None or passwd is None or port is None:
        return jsonify(results.req_info_incomplete_result)
    res = init_sys_service.get_hostname(ip, username, passwd, port)
    return jsonify(res)


# 接口作用： 设置目标主机的主机号
# 参数内容： ip、 username、 passwd、 port、 host
@web_bp.route("/hostname_set")
def hostname_set():
    ip = request.args.get("ip")
    username = request.args.get("username")
    passwd = request.args.get("password")
    port = request.args.get("port")
    new_hostname = request.args.get("hostname")
    res = init_sys_service.set_hostname(ip, username, passwd, port, new_hostname)
    return jsonify(res)


# 查看所有任务
@web_bp.route("/show_jobs")
def show_jobs():
    return jsonify(show_strategy_service())


# 修改执行的策略, 执行内容与add job相同，通过加入replace_existing=True，起到替换的作用
# 传入参数:
# data :{
#   id: 13
# }
@web_bp.route("/modify_strategy")
def modify_strategy():
    s_id = request.args.get('s_id')

    res = modify_strategy_service(s_id)
    return jsonify(res)


# 添加策略
@web_bp.route("/add_strategy")
def add_strategy():
    s_id = request.args.get('s_id')
    res = add_strategy_service(s_id)
    return jsonify(res)


# 删除策略
@web_bp.route("/remove_strategy")
def remove_strategy():
    """
    删除策略
    :return:
    """
    s_id = request.args.get('s_id')
    if s_id is None:
        return jsonify(results.req_info_incomplete_result)
    return jsonify(remove_strategy_service(s_id))


# 批量删除策略
@web_bp.route("/remove_strategy_batch")
def remove_strategy_batch():
    """
    资产批量删除接口
    :return:
    """
    ids = request.args.get("s_id")
    if ids is None:
        return jsonify(results.req_info_incomplete_result)
    id_list = ids.split(",")
    err_list = []
    for s_id in id_list:
        res = remove_strategy_service(s_id)
        if res['code'] != 200:
            err_list.append(s_id)
    if len(err_list) == 0:
        return jsonify(results.success_result)
    else:
        res = results.strategy_not_exist_result
        err_id_str = ','.join(err_list)
        res['data'] = f'策略不存在， ID: {err_id_str}'
        return res


@web_bp.route("/system_configure")
def system_configure():
    """
    获取系统配置信息（cpu使用率、内存使用率、磁盘使用率、磁盘剩余）
    :return:
    """
    res = configure_service.get_system()
    return jsonify(res)






